package com.just.recruitback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.just.recruitback.entity.User;
import com.just.recruitback.service.UserService;
import com.just.recruitback.service.base.IUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : baibaiwucahng
 * @date : 2020/7/4  21:54
 */
@Service
public class UserServiceImpl implements UserService{
    @Resource
    private IUserService iUserService;

    @Override
    public int register(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        User userServiceOne = iUserService.getOne(queryWrapper);
        if (ObjectUtils.isNotEmpty(userServiceOne)) {
            return 1;
        }
        iUserService.save(user);
        return 0;
    }

    @Override
    public int login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        User userServiceOne = iUserService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(userServiceOne)) {
            return -1;
        }
        if (ObjectUtils.notEqual(user.getPassword(), userServiceOne.getPassword())) {
            return -2;
        }
        return userServiceOne.getId();
    }

    @Override
    public void edit(User user) {
        iUserService.updateById(user);
    }
}
